package me.shiqiang.im.linsteners;

import com.corundumstudio.socketio.SocketIOClient;
import com.corundumstudio.socketio.annotation.OnConnect;
import com.corundumstudio.socketio.annotation.OnDisconnect;
import com.corundumstudio.socketio.annotation.OnEvent;
import me.shiqiang.im.common.SocketClientMap;
import me.shiqiang.im.enums.MessageCmd;
import me.shiqiang.im.msg.Login;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * @author cuishiqiang
 * @date 2017/12/12 下午1:43
 */
@Component("LoginListener")
public class LoginListener extends Listener {
    private Logger logger = LoggerFactory.getLogger(this.getClass());



    @OnEvent(MessageCmd.LOGIN)
    public void onLogin(SocketIOClient client, Login login) {
        Map<String, SocketIOClient> map = new HashMap<>();
        client.set("serverId",String.valueOf(login.getUserId()));
        client.set("machineId",String.valueOf(login.getPwd()));
        map.put("add",client);
        SocketClientMap.getInstance().put(map);
        logger.info("接收business建立连接serverId:{},machineId:{}",login.getUserId(),login.getPwd());
    }

    @OnConnect
    public void onConnect(SocketIOClient client) {
//        long count = LoginCount.getInstance().increment();
//        logger.info("在线人数 {}", count);
//        System.out.println(client.getRemoteAddress() + " connected!");
    }

    @OnDisconnect
    public void onDisconnect(SocketIOClient client) {
        Map<String, SocketIOClient> map = new HashMap<>();
        map.put("sub",client);
        SocketClientMap.getInstance().put(map);
    }
}
